package shaw.wifi.gui;

import some.pkg.R;
import android.app.Activity;
import android.text.method.ScrollingMovementMethod;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.Button;
import android.widget.CheckBox;
import android.widget.ProgressBar;
import android.widget.TextView;

public class SecureConnectGui {
	//variables for the ui elements
	private TextView result = null;
	private ProgressBar connectProgress = null;
	private CheckBox disableOpen = null;
	private Button connect;
	
	//the GUI is a singleton
	private static SecureConnectGui instance;
	public static SecureConnectGui get() {
		if(instance == null) {
			instance = new SecureConnectGui();
		}
		return instance;
	}
	
	//private function to setup the GUI element variables to specified values.
	//you won't have to call this ever, instead, use the setupGui in the main activity
	public void setupGui(TextView result, ProgressBar connectProgress, CheckBox disableOpen, Button connect, OnClickListener cl) {
		//setup variables
		this.result = result;
		result.setMovementMethod(new ScrollingMovementMethod());
		this.connectProgress = connectProgress;
		this.disableOpen = disableOpen;
		this.connect = connect;
		
		//create the action listener for the button
		if(cl != null) {
			this.connect.setOnClickListener(cl);
		}
		
		//set the current gui state to available
		setAsAvailable();
	}
	
	public static void setupGui(Activity authActivity) {
		TextView result = (TextView) authActivity.findViewById(R.id.resultTv);
		ProgressBar connectProgress = (ProgressBar) authActivity.findViewById(R.id.connectProgress);
		CheckBox disableOpen = (CheckBox) authActivity.findViewById(R.id.disableOpenCB);
		Button connect = (Button) authActivity.findViewById(R.id.submitBtn);
		get().setupGui(result, connectProgress, disableOpen, connect, new ConnectButtonListener());
	}
	
	public void setAsConnecting() {
		//disable the GUI elements so that they cannot be used
		disableOpen.setEnabled(false);
		//show the spinning progress bar thing
		connectProgress.setVisibility(View.VISIBLE);
		//set the button's text to Cancel
		connect.setText("Cancel");
	}
	
	public void setAsAvailable() {
		//enable the GUI elements so that they can be used
		disableOpen.setEnabled(true);
		//show the spinning progress bar thing
		connectProgress.setVisibility(View.INVISIBLE);
		//set the button's text to Cancel
		connect.setText("Setup Network");
	}
	
	public boolean getDisableOpen() {
		return disableOpen.isChecked();
	}
	
	public void addLineToOutput(String line) {
		if(result != null) {
			result.setText(line + "\n" + result.getText());
		}
    }
	
	public void disableButton() {
		connect.setEnabled(false);
	}
}
